<?php

namespace app\admin\model\shop\notify;

use think\Model;

/**
 * NotifyConfig
 */
class NotifyConfig extends Model
{
    // 表名
    protected $name = 'shop_notify_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'receiver',
    ];


    public function getReceiverAttr($value, $row): array
    {
        $receiverIds = $row['receiver_ids'] ?? '';

        if (!$receiverIds) return [];

        return [
            'username' => \app\admin\model\Admin::whereIn('id', $row['receiver_ids'])->column('username'),
        ];
    }

    public function getReceiverIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setReceiverIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }
}